/*
 * PositiveIntegerOnly.java
 *
 * Created on Feb 27, 2008
 *
 * Copyright(c) 2008 OpenOak Technologies 
 * Licensed under the OpenOak Learning License
 */

package it.allerj.gui.component;

import com.openoak.textfield.Plain;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *  Creates an Positive integer-only text field
 *
 *  @author Steven C. Daniel - Copyright (c) 2008
 *  @author OpenOak Technologies - www.openoak.com
 *  @version 1.0
 */
public class TextOnly extends Plain implements FocusListener {
    private java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("it/allerj/bundle/allerj");
    private String message = bundle.getString("i18n.text.only");

    
    public TextOnly() {
        this.setDocument(new TextDocument());
        this.setHorizontalAlignment(TextOnly.LEFT);
        this.addFocusListener(this);
    }

    
       
    private void showMyMessage(String message) {
        this.showMessage(message);
    }
    
    private void showMyMessageWarning(String message) {
        this.showMessageWarning(message);
    }
    
    private void showMyMessageError(String message) {
        this.showMessageError(message);
    }
    
    private void hideMyMessage(){
        this.clearMessage();
    }
    
    /**
     * The 'document' that controls the text input
     */
    class TextDocument extends PlainDocument {
        private boolean validInput = false;
        public TextDocument(){
            
        }
        
        @Override
        public void insertString(int offset, String string, AttributeSet attributes) throws BadLocationException {
            if (string == null) {
                return;
            } else {
                String newValue;
                
                int length = getLength();
                if (length == 0) {
                    newValue = string;
                } else {
                    String currentContent = getText(0, length);
                    StringBuffer currentBuffer = new StringBuffer(currentContent);
                    currentBuffer.insert(offset, string);
                    newValue = currentBuffer.toString();
                }
                checkInput(newValue, offset);
                super.insertString(offset, string, attributes);
            }
        }
        
        @Override
        public void remove(int offset, int length) throws BadLocationException {
            int currentLength = getLength();
            String currentContent = getText(0, currentLength);
            String before = currentContent.substring(0, offset);
            String after = currentContent.substring(length + offset, currentLength);
            String newValue = before + after;
            checkInput(newValue, offset);
            super.remove(offset, length);
        }
        
        public String checkInput(String proposedValue, int offset) throws BadLocationException {
            if(proposedValue == null) return "";
            int proposedLength = proposedValue.length();
            validInput = false;
            if (proposedLength > 0) {
                boolean isOK = false;
                String regEx = "[a-zA-Z ]";
                for (int i = 0; i < proposedLength; i++){
                    char checkChar = proposedValue.charAt(i);

                    Pattern maskPattern = Pattern.compile(regEx);
                    String maskMatch = String.valueOf(checkChar);
                    Matcher matcher = maskPattern.matcher(maskMatch);
                    if (matcher.find()) {
                        isOK = true;
                    } else {
                        isOK = false;
                        showMyMessageWarning(bundle.getString("i18n.insert.text.only"));
                        throw new BadLocationException(proposedValue, offset);
                    }
                    
                }
                if (isOK) {
                    showMyMessage(message);
                }
                
                validInput = isOK;
            }
            return proposedValue;
        }
        public boolean isValidInput(){
            return validInput;
        }
    }

    @Override
    public void focusGained(FocusEvent e) {
        super.focusGained(e);
        showMyMessage(message);
        renderValidText();
    }
    
    @Override
    public void focusLost(FocusEvent e) {
        super.focusLost(e);
        if(this.getText()!=null){
            super.setText(this.getText());
            if(isValidText()){
                hideMyMessage();
            }
        } else {
            super.setText("");
        }
    }
    @Override
     public boolean isValidText(){
            return ((TextDocument)this.getDocument()).isValidInput();
        }
    
    
}
